package com.sprucetec.live.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sprucetec.live.base.entity.User;
import com.sprucetec.live.common.dto.UserDto;
import com.sprucetec.live.common.exceptioin.SprucetecException;
import com.sprucetec.live.common.util.*;
import com.sprucetec.live.common.webcontext.LoginUserContext;
import com.sprucetec.live.service.UserService;
import com.sprucetec.live.util.SsoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 用户信息、角色、权限城市等信息获取
 * @author: kenny
 * @version v2.0
 */
@Slf4j
@Controller
@RequestMapping("/user")
public class UserInfoController extends BaseController{

	@Value(value = "${auth.serverurl}")
	String authServerUrl;

	@Autowired
	private UserService userService;

    /**
     * @Description 获取用户及角色信息
     * @param
     * @return String
     */
	@RequestMapping(value = "/getUserInfo",produces = "application/json; charset=utf-8")
	@ResponseBody
	public ServiceResult<User> getUserInfo() {
        User userInfo = SsoUtil.getLoginUser();
        log.info("getUserInfo out::User="+userInfo);
        if (userInfo == null) {
        	return ServiceResultUtil.fail(RespCode.ERROR, "未获取到用户信息");
        }
        return ServiceResultUtil.success(true,1,"获取用户信息成功",userInfo);
    }

    /**
     * @Description 搜索用户
     * @param query
     * @return ServiceResult<List<Map<String,String>>>
     */
    @RequestMapping(value = "/queryAuthEmail", method = RequestMethod.GET)
	@ResponseBody
	public ServiceResult<List<Map<String, String>>> queryAuthEmail(String query, HttpServletRequest request) {

		if (StringUtils.isBlank(query)) {
			query = SsoUtil.getLoginUser().getEmail();
		}
		//获取用户真实ip
		String realIp = OssUtil.getRealIp(request);
		if(StringUtils.isBlank(realIp)){
			throw new SprucetecException("获取用户ip失败");
		}
		Map<String, String> param = new HashMap<>();
		param.put("email", query);
		param.put("real_ip", query);
		String returnMsg = HttpClient.post(authServerUrl + "/adminuser/searchadmin",param);
		JSONObject object = JSONObject.parseObject(returnMsg);
		if (!object.getInteger("ret").equals(1)) {
			return ServiceResultUtil.fail(RespCode.ERROR, "调用auth系统adminuser/searchadmin接口异常");
		}

		JSONArray jsonArr = object.getJSONArray("data");

		Iterator<Object> it = jsonArr.iterator();
		List<Map<String, String>> list = new ArrayList<>();
		while (it.hasNext()) {
			JSONObject ob = (JSONObject) it.next();
			Map<String, String> map = new HashMap<>();
			if(ob.containsKey("uid") && StringUtils.isNotBlank(ob.getString("uid"))){
				map.put("value", ob.getString("name")+"-"+ob.getString("email"));
				map.put("lable", ob.getString("uid"));
				list.add(map);
			}
		}
		return ServiceResultUtil.success(list);
	}

    /**
     * 搜所用户详细信息
     * @param
     * @return ServiceResult<List<Map<String, String>>>
     */
    @RequestMapping(value = "/queryUserByInputParam", method = RequestMethod.GET)
   	@ResponseBody
   	public ServiceResult<List<Map<String, String>>> queryUserByInputParam(String query) {

    	if (StringUtils.isBlank(query)) {
			query = SsoUtil.getLoginUser().getEmail();
		}
		return ServiceResultUtil.success(userService.fuzzyQueryUserList(query));
   	}

	/**
	 *获取用户详情信息
	 * @return
	 */
	@RequestMapping(value = "/getUserInfoByUid",produces = "application/json; charset=utf-8")
	@ResponseBody
	public ServiceResult<UserDto> getUserInfoByInputParam() {
		String uid = LoginUserContext.getUser().getUid();
		return ServiceResultUtil.success(userService.queryUserByInput(uid));
	}

}
